package de.fhd.pms.ctrl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.fhd.pms.dao.KundeDao;
import de.fhd.pms.dao.KundeDeleteException;
import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Kunde;

/**
 * The Controller class for {@link Kunde} objects. 
 * Its tasks:
 * <ul>
 * <li>read and verify the parameters</li>
 * <li>access to the objects of the system</li>
 * <li>selection of the view and provision of content for the view</li>
 * </ul>
 * 
 * @author Fischer, Neetzel & Six
 *
 */
@Controller
public class KundeController {
	private static Logger log = Logger.getLogger(KundeController.class);
	// von mir gemacht f�r KundeDeleteException!
	private KundeDao kundeDao = null;

	public void setKundeDao(KundeDao kundeDao) {
		this.kundeDao = kundeDao;
	}

	/**
	 * For every request for this controller, this will create a kunde instance
	 * for the form.
	 */
	// @ModelAttribute
	// public Kunde newRequest(@RequestParam(required=false) Integer id) {
	// return (id != null ? kundeDao.findById(id) : new Kunde());
	// }

	/**
	 * <p>
	 * Kunde form request.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/RegisterKunde.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SetKunde.html", method = RequestMethod.GET)
	public ModelAndView set(@RequestParam(required = false) Integer id) {
		log.debug("SetKunde.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Kunde());
		} else {
			mv.addObject(kundeDao.findById(id));
		}
		mv.setViewName("kunde-set-kunde");
		return mv;
	}
	
	/**
	 * <p>
	 * Kunde form request.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/EditKunde.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/EditKunde.html", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required = false) Integer id) {
		log.debug("EditKunde.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Kunde());
		} else {
			mv.addObject(kundeDao.findById(id));
		}
		mv.setViewName("admin-edit-kunde");
		return mv;
	}

	/**
	 * <p>
	 * Saves a kunde.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveKunde.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SaveKunde.html", method = RequestMethod.POST)
	public String save(Kunde kunde, Model model) {
		if (kunde.getCreated() == null) {
			kunde.setCreated(new Date());
		}
		Kunde result = kundeDao.save(kunde);
		// set id from create
		if (kunde.getId() == null) {
			kunde.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
		return "redirect:SuccessKunde.html";
	}
	
	//neue methode für getrennte Speicherung im Adminbereich
	@RequestMapping(value = "/AdminSaveKunde.html", method = RequestMethod.POST)
	public String saveByAdmin(Kunde kunde, Model model) {
		if (kunde.getCreated() == null) {
			kunde.setCreated(new Date());
		}
		Kunde result = kundeDao.save(kunde);
		// set id from create
		if (kunde.getId() == null) {
			kunde.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "kunde.form.msg.success");
		return "redirect:AlleKunden.html";
	}
	
	/**
	 * <p>
	 * Deletes a kunde.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/LoescheKunde.html'.
	 * </p>
	 */
//	Exception Versuch
//	@RequestMapping(value = "/LoescheKunde.html", method = RequestMethod.GET)
//	public String delete(Kunde kunde) {
//		try {
//			kundeDao.delete(kunde);
//
//		} catch (KundeDeleteException e) {
//			e.getMessage("test");
//		}
//
//		return "redirect:AlleKunden.html";
//	}
	
//	ORIGINAL
    @RequestMapping(value="/LoescheKunde.html", method=RequestMethod.GET)
    public String delete(Kunde kunde) {
        kundeDao.delete(kunde);
        return "redirect:AlleKunden.html";
    }

	/**
	 * <p>
	 * Searches for all kundes and returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AlleKunden.html'.
	 * </p>
	 */
	@RequestMapping(value = "/AlleKunden.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Kunde bearbeiten");
		mv.addObject("message", "Alle Kunden");
		mv.addObject("kunden", kundeDao.findAll());
		mv.setViewName("admin-list-kunde");
		return mv;
	}
	
	/**
	 * <p>
	 * Searches for all kundes and returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/SuccessKunde.html'.
	 * </p>
	 */
	@RequestMapping(value = "/SuccessKunde.html", method = RequestMethod.GET)
	public ModelAndView success() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Erfolgreich!");
		mv.addObject("message", "Ihre Kundendaten wurden erfolgreich angelegt.");
		mv.setViewName("kunde-set-kunde-success");
		return mv;
	}

}
